import request from '@/utils/request';
import type { ItemInfo } from './item';
import type { WarehouseInfo } from './warehouse';
// import type { UserInfo } from './user'; // 移除错误的导入

// 简单的 UserInfo 结构（由于 user.ts 未导出 UserInfo，在此定义）
interface UserInfo {
    userId: number;
    username: string;
    realName?: string;
}

// 出库记录信息
export interface OutboundRecordInfo {
  outboundId: number;
  itemId: number;
  item: ItemInfo; // 关联的物品信息
  warehouseId: number;
  warehouse: WarehouseInfo; // 关联的仓库信息
  quantity: number;
  unitPrice: number;
  totalAmount: number;
  recipient: string; // 领用人/接收单位
  operatorId: number;
  operator: UserInfo; // 关联的操作员信息
  outboundTime: string; // 出库时间
  notes: string; // 备注
  createdAt: string;
}

// 查询参数
export interface OutboundRecordListParams {
  page?: number;
  pageSize?: number;
  itemId?: number; // 注意这里的字段名是 itemId
  warehouseId?: number; // 注意这里的字段名是 warehouseId
  startTime?: string;
  endTime?: string;
}

// 返回结构
export interface OutboundRecordListResponse {
  list: OutboundRecordInfo[];
  total: number;
  page: number;
  pageSize: number;
}

// 创建出库记录参数
export interface CreateOutboundRecordParams {
  itemId: number; // 注意这里的字段名是 itemId
  warehouseId: number; // 注意这里的字段名是 warehouseId
  quantity: number;
  unitPrice: number;
  recipient: string;
  notes?: string;
}

// 创建出库记录返回结果
export interface CreateOutboundRecordResponse {
  outboundId: number; // 注意这里的字段名是 outboundId
  itemId: number; // 注意这里的字段名是 itemId
  warehouseId: number; // 注意这里的字段名是 warehouseId
  quantity: number;
  unitPrice: number; // 注意这里的字段名是 unitPrice
  totalAmount: number; // 注意这里的字段名是 totalAmount
  recipient: string;
  operatorId: number; // 注意这里的字段名是 operatorId
  outbound_time: string; // 注意这里的字段名是 outboundTime
  notes?: string;
  createdAt: string; // 注意这里的字段名是 createdAt
}

export interface OutboundRecordDetailResponse {
  code: number;
  message: string;
  data: OutboundRecordInfo;
}

// Add interfaces for Update
export interface UpdateOutboundRecordParams {
  outboundId: number;
  itemId: number;
  warehouseId: number;
  quantity: number;
  unitPrice: number;
  outboundTime: string; // Assuming outboundTime is required for update
  operatorId: number; // Assuming operatorId is required for update
}

export interface UpdateOutboundRecordResponse {
  code: number;
  message: string;
  data?: any; // Adjust based on actual API response
}

// Add interfaces for Delete
export interface DeleteOutboundRecordResponse {
  code: number;
  message: string;
  data?: any; // Adjust based on actual API response
}

// 获取出库记录列表
export function getOutboundRecordList(params: OutboundRecordListParams) {
  return request.get<OutboundRecordListResponse>('/getOutboundRecords', { params });
}

const outboundApi = {
  // 获取单个出库记录
  getOutboundRecordById(id: number) {
    // 根据返回结果，使用 OutboundRecordInfo 类型（与列表返回结构一致）
    return request.get<OutboundRecordDetailResponse>(`/OutboundRecordsById/${id}`);
  },
  getOutboundRecordList(params: OutboundRecordListParams) {
  return request.get<OutboundRecordListResponse>('/getOutboundRecords', { params });
  },
  // 创建出库记录
  createOutboundRecord(data: CreateOutboundRecordParams) {
    return request.post<CreateOutboundRecordResponse>('/createOutboundRecord', data);
  }
};

export default outboundApi; 